package edu.ped.android.nfc;

import edu.ped.android.utils.NFCUtil;
import edu.ped.android.utils.PayloadTag;
import edu.ped.android.view.InitMatriceView;
import edu.ped.android.view.MyNumberPicker;
import greendroid.app.GDActivity;
import greendroid.widget.ActionBarItem;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.widget.TextView;

public class InitialisationActivity extends GDActivity {

	MyNumberPicker m_pickerxValue;
	MyNumberPicker m_pickeryValue;
	Button m_valid;
	TextView m_xShow;
	TextView m_yShow;
	View m_matrice;

	// Taille matrice
	int m_xValue;
	int m_yValue;

	// Indice des tags
	int xId=0;
	int yId=0;

	// Lignes pair,impair
	boolean odd = true;

	private Context mContext;
	boolean learningCurrent = false;

	// Progress bar
	public static final int MSG_NEXT = 0;
	public static final int MSG_END = 1;
	public static final int MSG_START = 2;
	public static final int DEFAULT_X = 2;
	public static final int DEFAULT_Y = 2;

	ProgressDialog mProgressDialog;
	InitMatriceView m_matriceView;

	@Override
	protected void onResume() {
		super.onResume();
		NFCUtil.getNFCFocus(this);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.activity_init_view);
		mContext = this;
		/*Title Bar*/
		ActionBarManager.initActionBar(this,ActionBarManager.INITALIZATION);

		m_pickerxValue =  (MyNumberPicker) findViewById(R.init_view.pickedwidth);
		m_pickeryValue = (MyNumberPicker) findViewById(R.init_view.pickedheight);
		m_matriceView = (InitMatriceView) findViewById(R.init_view.matriceview);
		m_matriceView.setPickerX(m_pickerxValue);
		m_matriceView.setPickerY(m_pickeryValue);
		m_pickerxValue.addObserver(m_matriceView);
		m_pickeryValue.addObserver(m_matriceView);
		m_valid = (Button) findViewById(R.init_view.initValid);
		m_pickerxValue.setValue(DEFAULT_X);
		m_pickeryValue.setValue(DEFAULT_Y);	

		OnClickListener launch = new OnClickListener() {
			public void onClick(View v) {
				m_xValue = m_pickerxValue.getValue()-1;
				m_yValue = m_pickeryValue.getValue()-1;
				xId=0;
				yId=0;
				learningCurrent = true;
				compute();
			}
		};
		m_valid.setOnClickListener(launch);		
	}


	@Override
	protected void onNewIntent(Intent intent) {
		if (learningCurrent) {
			if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())
					|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())
					|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
				/* Object */
				try {
					if (NFCUtil.writeTagObject(new PayloadTag(yId, xId,m_yValue+1,m_xValue+1,null), intent)) {	
						if(odd){
							if(xId < m_xValue)
								xId++;
							else{
								yId++;
								odd = false;
							}
						}
						else{
							if(xId > 0)
								xId--;
							else{
								yId++;
								odd = true;
							}
						}
						// Arret 
						if (yId > m_yValue){
							learningCurrent = false;
							// populates the message
							Message msg = mHandler.obtainMessage(MSG_END, getResources().getString(R.string.complete));
							// sends the message to our handler
							mHandler.sendMessage(msg);
						}
						// populates the message
						Message msg = mHandler.obtainMessage(MSG_NEXT, new String(getResources().getString(R.string.goTo)+" "+xId +" "+yId));
						// sends the message to our handler
						mHandler.sendMessage(msg);
					}else {
						Toast.makeText(this, R.string.write_fail, Toast.LENGTH_LONG).show();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		} 

	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		if(!ActionBarManager.handleActionBar(this, item, position))
			return super.onHandleActionBarItemClick(item, position);
		return true;
	}

	private void compute() {
		mProgressDialog = ProgressDialog.show(this, getResources().getText(R.string.wait), getResources().getText(R.string.goToFirst), true);
		// useful code, variables declarations...
		new Thread(new Runnable() {

			public void run() {
				Message msg = null;
				String progressBarData = getResources().getString(R.string.goTo)+" "+xId +" "+yId;
				// populates the message
				msg = mHandler.obtainMessage(MSG_START, progressBarData);

				// sends the message to our handler
				mHandler.sendMessage(msg);
			}
		});
	}

	final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			String text2display = null;
			switch (msg.what) {
			case MSG_START:
				if (mProgressDialog.isShowing()) {
					mProgressDialog.setMessage(((String) msg.obj));
				}
				break;
			case MSG_NEXT:
				text2display = (String) msg.obj;
				mProgressDialog.setMessage(((String) msg.obj));
				break;
			case MSG_END:
				text2display = (String) msg.obj;
				Toast.makeText(mContext, "Info :" + text2display,
						Toast.LENGTH_LONG).show();
				if (mProgressDialog.isShowing()) {
					mProgressDialog.dismiss();
				}
				break;
			default: 
				break;
			}
		}
	};

}
